package longrise.phone.com.bjjt_henan.compensate;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * 保险报案后，温馨提示，选择线上，还是线下定损，Created by Administrator on 2016/5/12.
 */

public class PromptAfterInsuranceReportView extends LFView implements View.OnClickListener {
    private String singlecar;
    private Context context = null;
    private View view = null;
    private Button bt_enterconfirmlose1;
    private Button bt_enterconfirmlose2;
    private EntityBean[] CreateAgreementbean;
    private String appCaseno;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private BaseApplication application;
    private EntityBean[] cpBeans;//保险公司集合

    public PromptAfterInsuranceReportView(Context c, EntityBean[] CreateAgreementbean, String appCaseno) {
        super(c);
        this.context = c;
        this.CreateAgreementbean = CreateAgreementbean;
        this.appCaseno = appCaseno;
    }

    public PromptAfterInsuranceReportView(Context c, EntityBean[] CreateAgreementbean, String appCaseno, String singlecar) {
        super(c);
        this.context = c;
        this.CreateAgreementbean = CreateAgreementbean;
        this.appCaseno = appCaseno;
        this.singlecar = singlecar;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);


    }

    private void initView() {
        view = View.inflate(context, R.layout.view_promptafterinsurancereport, null);
        bt_enterconfirmlose1 = (Button) view.findViewById(R.id.bt_enterconfirmlose1);
        bt_enterconfirmlose2 = (Button) view.findViewById(R.id.bt_enterconfirmlose2);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("温馨提示");

        application = (BaseApplication) context.getApplicationContext();
    }

    private void regEvent(boolean b) {
        if (null != bt_enterconfirmlose1) {
            bt_enterconfirmlose1.setOnClickListener(b ? this : null);
        }
        if (null != bt_enterconfirmlose2) {
            bt_enterconfirmlose2.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_enterconfirmlose1:
                if (null == cpBeans) {//未获取到保险公司集合
                    getCplist();
                    return;
                }
                reportInsurance();
                break;
            case R.id.bt_enterconfirmlose2:
                //TODO 进入理赔中心(百度地图)
                BaiduMapView baidumapview = new BaiduMapView(context);
                FrameworkManager.getInstance().showNewForm(context, baidumapview);
                break;
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;
        }
    }

    //进入保险报案
    private void reportInsurance() {
        InsuranceReportView insurancereportview = null;
        if (TextUtils.equals("0", singlecar)) {
            insurancereportview = new InsuranceReportView(context, CreateAgreementbean, appCaseno, "0");
        } else {
            insurancereportview = new InsuranceReportView(context, CreateAgreementbean, appCaseno);
        }
        insurancereportview.setCpBeans(cpBeans);
        FrameworkManager.getInstance().showNewForm(context, insurancereportview);
    }

    //加载保险公司
    private void getCplist() {
        String userflag = application.getKckpName();
        String token = application.getToken();
        DialogUtils.showDialog(context);
        ApiImpI.getInstance(context).searchCplist(userflag, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(context, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    cpBeans = bean.getBeans("data");
                    if (null != cpBeans) {
                        reportInsurance();
                        return;
                    }
                }
                String redes = bean.getString("redes");
                if (!TextUtils.isEmpty(redes)) {
                    UiUtil.showToast(context, redes);
                }
            }
        });
    }
}
